package com.sys.midware.assist.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 类型转换工具类 <b>ClassName：</b> ConvertUtil <br/>
 * <b>Description：</b> TODO <br/>
 * <b>@author：</b> mobing <br/>
 * <b>@date：</b> 2016年1月21日 下午7:26:47 <br/>
 * <b>@version: </b> <br/>
 */
public class ConvertUtil {

    /**
     * Converts a map to a JavaBean.
     * 
     * @param type
     *            type to convert
     * @param map
     *            map to convert
     * @return JavaBean converted
     */
    public static final <T> T MapToBean(Map<String, ? extends Object> map, Class<T> type) {
        if (map == null || type == null) {
            return null;
        }
        T obj = null;
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(type);
            obj = (T)type.newInstance();
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();
                if (map.containsKey(propertyName)) {
                    Object value = map.get(propertyName);
                    Object[] args = new Object[1];
                    args[0] = value;
                    descriptor.getWriteMethod().invoke(obj, args);
                }
            }
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
    
    
    /**
     * Converts a map to a JavaBean.
     * exclude null
     * @param type
     *            type to convert
     * @param map
     *            map to convert
     * @return JavaBean converted
     */
    public static final Object MapToBeanEXNull(Map<String, ? extends Object> map, Class<?> type) {
        if (map == null || type == null) {
            return null;
        }
        Object obj = null;
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(type);
            obj = type.newInstance();
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();
                if (map.containsKey(propertyName)) {
                    Object value = map.get(propertyName);
                    if(value==null){
                        continue;
                    }
                    Object[] args = new Object[1];
                    args[0] = value;
                    descriptor.getWriteMethod().invoke(obj, args);
                }
            }
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    /**
     * Converts a JavaBean to a map.
     * 
     * @param bean
     *            JavaBean to convert
     * @return map converted
     */
    public static final Map<String, Object> BeanToMap(Object bean) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if(bean ==null){
            return returnMap;
        }
       
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();
                if (!propertyName.equals("class")) {
                    Method readMethod = descriptor.getReadMethod();
                    Object result = readMethod.invoke(bean, new Object[0]);
                    if (result != null) {
                        returnMap.put(propertyName, result);
                    } else {
                        returnMap.put(propertyName, "");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return returnMap;

    }


}
